
App({
  onLaunch: function () {
    this.globalData = {
      userInfo: null,
      authType: 4,
      code: ""
    }
    //获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              // console.log(res);
              this.globalData.userInfo = res.userInfo
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },

  onShow: function (options) {
    if (options.scene == 1007) {
      // 通过单人聊天会话分享进入
    }
    if (options.scene == 1008) {
      // 通过群聊会话分享进入
    }
    if (options.scene == 1001) {
      // 通过发现栏小程序进入
    }
  },

  // 判断是否登录
  isLogin() {
    var uid = wx.getStorageSync('uid');
    console.log(uid);
    if (!uid) {
      wx.showModal({
        title: "温馨提示",
        showCancel: true,
        content: "请先登录!",
        confirmText: "去登录",
        success(res) {
          if (res.confirm) {
            console.log("登录");
            wx.redirectTo({
              url: '/packageA/login/login',
            })
          } else if (res.cancel) {
            console.log("取消");
          }
        }
      })
      console.log("false");
      return false
    }
    console.log("true");
    return true
  },
  // 查询订阅权限函数
  checkSub(params) {//传入tmplId，检测用户是否开放权限，允许推送消息
    var tmplIds = params.tmplIds
    return new Promise((resolve, reject) => {
      wx.getSetting({
        withSubscriptions: true,
        success(res) {
          console.log(res)
          if (res.subscriptionsSetting.itemSettings != undefined) {
            var flag = res.subscriptionsSetting.itemSettings[tmplIds[0]]
          } else {
            var flag = undefined
          }
          console.log(flag)
          if (flag == undefined) {
            wx.requestSubscribeMessage({
              tmplIds: tmplIds,
              success(res) {
                console.log(res)
                //点击完成后就返回成功就行
              },
              fail: err => {
                console.log(err);
              }
            })
          } else if (flag != 'accept') {
            wx.requestSubscribeMessage({
              tmplIds: tmplIds,
              success(res) {
                console.log(res)
                resolve(true)//点击完成后就返回成功就行
              },
              fail: err => {
                console.log(err);
              }
            })
          } else {//直接返回true,原本以为用户选择一直同意之后，就可以一直推送，这里是一个bug
            wx.requestSubscribeMessage({
              tmplIds: tmplIds,
              success(res) {
                console.log(res)
                resolve(true)//点击完成后就返回成功就行
              },
              fail: err => {
                console.log(err);
              }
            })
          }
        },
      })
    })
  },
  globalData: {
    userInfo: null,
    gtb: '',
    IsAgentHeHuoRen:'',//用于判断是否查看数据总览
    IsAgentHeHuoRen:'',//用于判断是否查看数据总览
    IsPartner:'',//用于判断是否查看数据总览
  },

})
